package cat.joronya.supermanager.service.substrImpl;

import java.util.ArrayList;

import cat.joronya.supermanager.market.Player;
import cat.joronya.supermanager.service.MarketBuyParser;

public class MarketBuyParserImpl implements MarketBuyParser 
{
	public ArrayList<Player> players = new ArrayList<Player>();
	
	// start position
	private static final String START_POSITION_STRING = "gif/fondomercadoarriba.gif";
	private static final String STOP_POSITION_STRING = "gif/fondomercadoabajo.gif";
	
	// start/end tr
	private static final String START_TR = "<tr>";
	private static final String STOP_TR = "</tr>";
	
	// start lesio
	private static final String START_LESIO = "gif/lesion.gif";
	
	// start baixa
	private static final String START_BAIXA = "gif/baja.gif";
	
	// start nacional
	private static final String START_NACIONAL = "gif/nacional.gif";
	
	// start extracomunitari
	private static final String START_EXTRA = "gif/extra.gif";
	
	// start codi
	private static final String START_CODI = "<a href=\"http://www.acb.com/stspartidojug.php?cod_jugador=";
	private static final String STOP_CODI = "\" target=";

	// start color
	private static final String START_COLOR = "<font color=\"";
	private static final String STOP_COLOR = "\">";
	
	// start info
	private static final String START_INFO = "onMouseover=\"ddrivetip('";
	private static final String STOP_INFO = "')\";";
	
	// start idjug
	private static final String START_IDJUG = "name=\"id_jug\" value=\"";
	private static final String STOP_IDJUG = "\"><input";
	
	public long parse(String html, Integer jornada) throws Exception 
	{
		long start = System.nanoTime();
		
		int startPosition = html.indexOf(START_POSITION_STRING);
		if( startPosition == -1 )
			throw new Exception();

		int stopPosition = html.indexOf(STOP_POSITION_STRING);

		// recorrem els tr, comencem pel 3er tr
		int startTr, stopTr;
		startTr = html.indexOf(START_TR, startPosition);
		startTr = html.indexOf(START_TR, startTr+START_TR.length());
		startTr = html.indexOf(START_TR, startTr+START_TR.length());
		while( startTr != -1 && startTr < stopPosition )
		{
			// recuperem tr
			stopTr = html.indexOf(STOP_TR, startTr);
			String tr = html.substring(startTr, stopTr);
			
			// code player
			int startCode = tr.indexOf(START_CODI);
			int stopCode = tr.indexOf(STOP_CODI, startCode);
			String cod = tr.substring(startCode+START_CODI.length(), stopCode);
			
			// color
			int startColor = tr.indexOf(START_COLOR);
			int stopColor = tr.indexOf(STOP_COLOR, startColor+START_COLOR.length());
			String color = tr.substring(startColor+START_COLOR.length(), stopColor);
			
			// info
			int startInfo = tr.indexOf(START_INFO, stopCode);
			String info = null;
			if( startInfo != -1 )
			{
				int stopInfo = tr.indexOf(STOP_INFO, startInfo+START_INFO.length());
				info = tr.substring(startInfo+START_INFO.length(), stopInfo);
			}
			// idjug
			String idJug = null;
			int startIdjug = tr.indexOf(START_IDJUG, stopCode);
			if( startIdjug != -1 )
			{
				int stopIdjug = tr.indexOf(STOP_IDJUG, startIdjug+START_IDJUG.length());
				idJug = tr.substring(startIdjug+START_IDJUG.length(), stopIdjug);
			}
			
			// creem player i el guardem per despres tractar-lo
			Player player = new Player();
			player.setDay(Integer.valueOf(jornada));
			player.setCodi(cod);
			player.setId(idJug);
			player.setShowInBuy(true);
			player.setColorInBuy(color);
			if( info != null )
				player.setInfo(info);
			
			// lesionat?
			if( tr.indexOf(START_LESIO) != -1 )
				player.setStatus("lesio.png");
			
			// baja?
			if( tr.indexOf(START_BAIXA) != -1 )
				player.setStatus("baixa.png");
			
			// nacional?
			if( tr.indexOf(START_NACIONAL) != -1 )
				player.setNacionality("nacional.png");
			
			// extra?
			if( tr.indexOf(START_EXTRA) != -1 )
				player.setNacionality("extra.png");
			
			// guardem player al array list
			players.add(player);

			// recuperem seguent startTr
			startTr = html.indexOf(START_TR, stopTr);
		}
		
		
		return System.nanoTime()-start;
	}
	
	public ArrayList<Player> getPlayers()
	{
		return players;
	}
}